package com.hall.controller;

import com.hall.pojo.SysUser;
import com.hall.service.RetrievPasswordServes;
import com.hall.service.RetrievPasswordServesImpl;
import com.hall.utils.Sendsms;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *修改个人密码 或 找回密码
 */
@Controller
@RequestMapping("/user")
public class PasswordOperationController {
    @Autowired
    RetrievPasswordServes retrievPasswordServes;
    private String numphone; //提交的手机号码
    @ResponseBody
    @RequestMapping("/sendmessage")  //发送验证码
    public String sendMessage(String phone, HttpSession session2) {
        System.out.println("++++++++++++++++++++++到方法了");
        numphone = phone;
        System.out.println("-------------------++++++++++++++++++++++" + phone);
        if (phone != null && phone.length() != 0) {
            Sendsms sendsms = new Sendsms();
            String text2 = sendsms.sendMessage(phone); //执行发送短信验证码方法，并且返回验证码给Str
            System.out.println("验证码为"+text2);
            session2.setAttribute(phone,text2);//固定字符串虽无使用问题，但改为用手机号码做key可防止高并发可能带来的问题
            session2.setMaxInactiveInterval(60);
            return "success";
        } else {
            return "false";  
        }
    }

    //表单提交执行的方法
    @ResponseBody
    @RequestMapping("/retrievePassword")  //phoneNumber输入的手机号  text2为输入的验证码
    public String RetrievePassword(String phoneNumber, String text2, String password,HttpSession session) {
        //判断获取验证码手机与提交的手机是否一致
        if (!phoneNumber.equals(numphone)) {
            System.out.println("++++++++++++++++++");
            System.out.println("两次手机不一致");
            return "false";
        }
        //判断输入验证码与接收的是否一致
        if (text2.equals(session.getAttribute(phoneNumber))) {
            int i = retrievPasswordServes.setPasswordByPhone(password, phoneNumber);
            if (i > 0) {
                return "ok";
            }
        }
        return "false";
    }

    @RequestMapping("/pagefind")
    public String pageFind(HttpServletRequest request, Model model3) {
        String p1 = numphone;
        SysUser all = retrievPasswordServes.getUsernameByPhone(p1);
        model3.addAttribute("lg2", all);
        return "findPassword_1";
    }
}


